<?php
/*
 *网易云签到功能类
 *Author：消失的彩虹海 & 訫言
*/
class API{
	// General
	protected $_MINI_MODE=false;
	protected $_MODULUS='00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7';
	protected $_NONCE='0CoJUm6Qyw8W8jud';
	protected $_PUBKEY='010001';
	protected $_VI='0102030405060708';
	protected $_USERAGENT='Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.157 Safari/537.36';
	protected $_COOKIE='os=pc; osver=Microsoft-Windows-10-Professional-build-10586-64bit; appver=2.0.3.131777; channel=netease; __remember_me=true;';
	protected $_REFERER='https://music.163.com/';
	// use static secretKey, without RSA algorithm
	protected $_secretKey='TA3YiYCfY2dDJQgg';
	protected $_encSecKey='84ca47bca10bad09a6b04c5c927ef077d9b9f1e37098aa3eac6ea70eb59df0aa28b691b7e75e4f1f9831754919ea784c8f74fbfadf2898b0be17849fd656060162857830e241aba44991601f137624094c114ea8d17bce815b0cd4e5b8e2fbaba978c6d1d14dc3d1faf852bdd28818031ccdaaa13a6018e1024e2aae98844210';

	// encrypt mod
	protected function prepare($raw){
		$data['params']=$this->aes_encode(json_encode($raw),$this->_NONCE);
		$data['params']=$this->aes_encode($data['params'],$this->_secretKey);
		$data['encSecKey']=$this->_encSecKey;
		return $data;
	}
	protected function aes_encode($secretData,$secret){
		return openssl_encrypt($secretData,'aes-128-cbc',$secret,false,$this->_VI);
	}

	// CURL
	protected function curl($url,$data=null,$cookie=null){
		$curl=curl_init();
		curl_setopt($curl,CURLOPT_URL,$url);
		if($data){
			if(is_array($data))$data=http_build_query($data);
			curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
			curl_setopt($curl,CURLOPT_POST,1);
		}
		curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($curl,CURLOPT_CONNECTTIMEOUT, 10);
		curl_setopt($curl,CURLOPT_REFERER,$this->_REFERER);
		curl_setopt($curl,CURLOPT_COOKIE,$this->_COOKIE.$cookie);
		curl_setopt($curl,CURLOPT_USERAGENT,$this->_USERAGENT);
		$result=curl_exec($curl);
		curl_close($curl);
		return $result;
	}

	// main function
	public function login($phone,$pwd){
		$url="https://music.163.com/weapi/login/cellphone";
		$data=array(
		"phone"=>$phone,
		"countrycode"=>"86",
		"password"=>md5($pwd),
		"rememberLogin"=>"true");
		return $this->curl($url,$this->prepare($data));
	}
	public function sign($cookie){
		$url="https://music.163.com/weapi/point/dailyTask";
		$data=array("type"=>0);
		return $this->curl($url,$this->prepare($data),$cookie);
	}
}